<template>
  <div class="pd-20 bg-fff radius-6 bg-fff">
    <el-row type="flex" class="mt-10 mb-10" justify="end">
      <router-link to="/role/add">
        <el-button type="primary" round>新增</el-button>
      </router-link>
    </el-row>
    <el-table
      ref="multipleTableRef"
      :data="tableData"
      tooltip-effect="dark"
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"></el-table-column>
      <el-table-column prop="id" width="100" label="编号"></el-table-column>
      <el-table-column prop="name" width="300" label="名称"></el-table-column>
      <el-table-column prop="value" label="角色"></el-table-column>

      <el-table-column prop="status" label="状态">
        <template #default="scope">
          {{ scope.row.status == "1" ? "正常" : "停用" }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="150" label="操作">
        <template #default="scope">
          <el-button
            :icon="Edit"
            :disabled="scope.row.id < 4"
            circle
            @click="toEdit(scope.row)"
          ></el-button>
          <el-button
            :icon="Delete"
            :disabled="scope.row.id < 4"
            circle
            @click="handleDelete(scope.row)"
          ></el-button>

          <el-tooltip
            v-if="scope.row.id < 4"
            content="系统默认角色，禁止删除"
            effect="light"
            placement="top-start"
          >
            <el-icon class="ml-10 t-4 pointer">
              <QuestionFilled class="c-165dff" />
            </el-icon>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-row type="flex" class="mt-20 align-c" justify="center">
      <el-pagination
        background
        layout="prev, pager, next"
        @current-change="handlePageChange"
        :page-size="10"
        :total="totalCount"
        hide-on-single-page
      ></el-pagination>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { list, del } from '@/api/sys_role.js';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { Delete, Edit, QuestionFilled } from '@element-plus/icons-vue';

const router = useRouter();
const multipleTableRef = ref(null);
const tableData = ref([]);
const multipleSelection = ref([]);
const totalCount = ref(0);
const currentPage = ref(1);

const fetchRoles = async () => {
  try {
    const res = await list(currentPage.value);
    if (res.code === 200) {
      tableData.value = [...res.data.list];
      totalCount.value = res.data.count;
    } else {
      ElMessage({
        message: res.msg,
        type: 'success',
      });
    }
  } catch (error) {
    console.log(error);
  }
};

const handlePageChange = (e) => {
  currentPage.value = e;
  fetchRoles();
};

// const toggleSelection = (rows) => {
//   if (rows) {
//     rows.forEach((row) => {
//       multipleTableRef.value.toggleRowSelection(row);
//     });
//   } else {
//     multipleTableRef.value.clearSelection();
//   }
// };

const handleSelectionChange = (val) => {
  multipleSelection.value = val;
};

const toEdit = (e) => {
  const id = e.id;
  router.push({ name: 'role-edit', params: { id: id } });
};

const handleDelete = async (e) => {
  const id = e.id;
  try {
    const res = await del(id);
    if (res.code === 200) {
      ElMessage({
        message: '删除成功 ^_^',
        type: 'success',
      });
      fetchRoles();
    } else {
      ElMessage({
        message: res.msg,
        type: 'success',
      });
    }
  } catch (error) {
    console.log(error);
  }
};

// 初始化时获取角色列表
onMounted(() => {
  fetchRoles();
});
</script>

<style scoped>
/* 添加样式 */
</style>
